<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 100px;
        height: 100px;
        border: 1px solid;
      }
      .active1 {
        width: 100px;
        height: 100px;
        background: yellow;
      }
      .active2 {
        width: 100px;
        height: 100px;
        background: cyan;
      }
      .active3 {
        width: 100px;
        height: 100px;
        background: greenyellow;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
    <button>点我</button>

    <script>
      var box = document.querySelector(".box");
      var btn = document.querySelector("button");
      var num = 0;
      btn.onclick = function () {
        num++;
        switch (num) {
          case 1:
            // box.style.backgroundColor = "blue";
            box.className = "box active1";
            //因为每个active里面给了宽高,所以替换掉box后宽高还在,div就还看得到,如果active只给了颜色,没有给宽高,那么把box替换为active的时候,盒子的宽高就没有了,盒子就不见了
            break;
          case 2:
            //box.style.backgroundColor = "cyan";
            box.className = "box active2";
            break;
          case 3:
            //box.style.backgroundColor = "greenyellow";
            box.className = "box active3";
            break;
          default:
            //box.style.backgroundColor = "black";
            break;
        }
        if (num == 3) {
          num = 0;
        }
      };
    </script>
  </body>
</html>
